package hkust.csit5110.android;

import hkust.csit5110.android.util.AndConstants;
import hkust.csit5110.android.util.GlobalChatListMap;
import hkust.lingx.csit5110.android.lib.model.AnchorMessage;
import hkust.lingx.csit5110.android.lib.server.NumbServer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * 
 * welcome view
 * 
 */
public class WelcomeView extends Activity {
  /** Called when the activity is first created. */
  private Button logonButton; // Logon button in UI1
  
  Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      String chatMessage = "";
      AnchorMessage anchor = (AnchorMessage) msg.obj;
      chatMessage += "From:[" + anchor.getMsgFrom() + "]";
      chatMessage += "To:[" + anchor.getMsgTo() + "]";
      chatMessage += "Content:[" + anchor.getMsgContent() + "]";
      GlobalChatListMap chatHist = (GlobalChatListMap)getApplicationContext();
      chatHist.addAnchorMessage(anchor.getMsgFrom(), anchor);
      Intent intent = new Intent(WelcomeView.this, ChatView.class);
      intent.putExtra("anchor", anchor);
      startActivity(intent);
      /**
       * copy from MatchAndChatView.java
     
      setContentView(R.layout.interface4);
      chatButton = (Button) findViewById(R.id.button0);
      chatButton.setContentDescription(anchor.getMsgFrom());
      input = (EditText) findViewById(R.id.editText1);
      show = (EditText) findViewById(R.id.editText2);
      ipadd = (EditText) findViewById(R.id.IP);
    
       
      chatButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          try {
            FeedBackClient c = new FeedBackClient();
            String connIp = "";
            connIp = v.getContentDescription().toString();
            if (connIp == null || connIp.trim().equalsIgnoreCase("") || connIp.trim().equalsIgnoreCase("null")) {
              connIp = ipadd.getText().toString();
            }
            c.setUpConn(connIp, AndConstants.CHAT_PORT);
            AnchorMessage m = new AnchorMessage();
            m.setMsgFrom("Stranger");
            m.setMsgTo("Server");
            m.setMsgContent(input.getText().toString());
            c.sendAnchorMessage(m);
            c.tearDownConn();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      });
      show = (EditText) findViewById(R.id.editText2);
      show.append(chatMessage);
      */
      super.handleMessage(msg);
    }
  };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.interface1);
    logonButton = (Button) findViewById(R.id.logonButton);
    logonButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(WelcomeView.this, RecordVoiceView.class);
        startActivity(intent);
      }
    });
    NumbServer server = new NumbServer(Integer.valueOf(AndConstants.CHAT_PORT));
    server.registerHandler(handler);
    Thread serverThread = new Thread(server);
    serverThread.start();
  }
}